import Toast from '../../component/vant/toast/toast';

const util = require('../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    grades: [],
    courses: [],
    gradeSelect: '',
    courseSelect: '',
    selectGrade: '',
    selectSubject: '',
    show: false,
    selected: 0,
    currentYear: '',
    currentYearID: '',
    years: [],
    coach_list: [],
    last_id: '',
    f1_type: 0,
    type: 1,
    show_sm_filter: true,
    checked: false,
    f1_type_txt: '全部',
    f_type_txt: '年份'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let grade_id = options.grade_id ? options.grade_id : '';
    let course_id = options.course_id ? options.course_id : '';

    //检查登录
    app.checkSession();

    var logging = wx.getStorageSync('_logining');

    if (!logging) {
      app.toAuth('/' + this.route);
      return false;
    }

    var page = this;
    //获取年份
    var years = util.getYears();
    page.setData({
      years: years
    });

    // 获取课程学科
    app.sendRequest({
      url: app.api.public.grade_course_student,
      success: function(res) {
        if (res.data != '') {
          let grade_courses = util.getCourses(res.data.list);
          // 设置数据
          grade_courses.map(g_course => {
            if (g_course.grade_id == grade_id) {
              page.setData({
                courses: g_course.course_list
              });
            }
          });
        }
      }
    });

    page.setData({
      gradeSelect: grade_id,
      courseSelect: course_id
    });

    app.setPageTitle(wx.getStorageSync('coach_gradeNameSelect'));

    page.initCoach();
  },
  closeShade() {
    this.setData({
      show: false
    });
  },
  initCoach: function() {
    var page = this;
    //辅导列表
    var param = {
      grade_id: page.data.gradeSelect == '' ? 0 : page.data.gradeSelect,
      course_id: page.data.courseSelect == '' ? 0 : page.data.courseSelect,
      year: page.data.currentYearID,
      type: page.data.type,
      order: page.data.f1_type
    };
    if (page.data.last_id != 0) {
      param.last_id = page.data.last_id;
    }
    app.sendRequest({
      data: param,
      url: app.api.coach.coach_student_list,
      success: function(res) {
        var coach_list = page.data.coach_list;
        var coach_list_load = res.data.list;
        coach_list_load == undefined ? [] : coach_list_load;
        var last_id = 0;
        if (coach_list_load != undefined && coach_list_load.length > 0) {
          last_id = coach_list_load[coach_list_load.length - 1].coach_id;
          coach_list = coach_list.concat(coach_list_load);
          page.setData({
            coach_list: coach_list,
            last_id: last_id
          });
        } else {
          page.data.last_id > 0 && Toast('没有更多了');
        }
      }
    });
  },
  swichCourse(e) {
    let page = this,
      course_id = e.currentTarget.dataset.id;
    page.setData({
      last_id: 0,
      coach_list: [],
      last_id: '',
      courseSelect: course_id,
      show: false
    });
    page.initCoach();
  },
  swichNav: function(e) {
    var cur = e.target.dataset.current,
      currentYearID = e.target.dataset.id,
      txt = e.target.dataset.txt;
    this.setData({
      f_type: '',
      f_type_txt: txt,
      last_id: 0,
      coach_list: [],
      currentYear: cur,
      currentYearID: currentYearID,
      show: false
    });
    this.initCoach();
  },
  toCoach: function(e) {
    var page = this,
      id = e.currentTarget.dataset.id;
    app.sendRequest({
      hideLoading: true,
      url: app.api.coach.coach_answer,
      data: { coach_id: id },
      success: res => {
        var url = '../coach_detail/coach_detail?id=' + id;
        var answer = res.data;
        page.setData({
          coach_id: id
        });
        if (answer != null && answer.finish != undefined) {
          url = '../coach_result/coach_result?id=' + id;
        }
        wx.navigateTo({
          url: url
        });
      }
    });
  },
  toggleCourse: function() {
    var page = this;
    page.setData({
      show: !page.data.show
    });
  },
  toggleFilter: function(e) {
    var page = this,
      f_type = e.currentTarget.dataset.type,
      show = false,
      old_f_type = page.data.f_type;
    if (!page.data.show) {
      show = true;
    } else {
      if (f_type == old_f_type) {
        f_type = '';
        show = false;
      } else {
        show = true;
      }
    }

    page.setData({
      show: show,
      f_type: f_type
    });
  },
  toggleSMFilter() {
    let page = this;
    page.setData({
      show_sm_filter: !page.data.show_sm_filter
    });
  },
  onChange(e) {
    let page = this;
    page.setData({
      checked: e.detail,
      type: e.detail ? 3 : 1
    });

    page.sureChoose();
  },
  sureChoose: function() {
    var page = this;
    page.setData({
      last_id: 0,
      coach_list: [],
      show: false
    });
    page.initCoach();
  },
  getFiltedData: function(e) {
    var page = this,
      f1_type = e.currentTarget.dataset.type;

    let txt_arr = ['全部', '最新', '最热'];
    page.setData({
      f_type: '',
      f1_type: f1_type,
      f1_type_txt: txt_arr[f1_type]
    });

    page.sureChoose();
  },
  chooseGrade: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }

    page.setData({
      selectGrade:
        e.currentTarget.dataset.select == ''
          ? '全部'
          : e.currentTarget.dataset.name,
      gradeSelect: e.currentTarget.dataset.select
    });
  },

  chooseSubject: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }
    page.setData({
      selectSubject:
        e.currentTarget.dataset.select == ''
          ? ''
          : e.currentTarget.dataset.name,
      courseSelect: e.currentTarget.dataset.select
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var page = this,
      coach_id = page.data.coach_id;
    if (coach_id != undefined && coach_id > 0) {
      app.sendRequest({
        hideLoading: true,
        url: app.api.coach.coach_answer,
        data: { coach_id: coach_id },
        success: res => {
          var answer = res.data;
          if (
            answer != null &&
            answer.finish != undefined &&
            answer.finish == 1
          ) {
            let coach_list = page.data.coach_list;
            coach_list.map(coach => {
              if (coach.coach_id == coach_id) {
                coach.status = 1;
              }
            });
            page.setData({
              coach_list: coach_list
            });
          }
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.initCoach();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
